<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 靠右的按钮 -->
        <page-tools>
          <template v-slot:after>
            <el-button type="primary" size="small" @click="addPermission('0', 1)">添加权限</el-button>
          </template>
        </page-tools>
        <!-- 表格 -->
        <el-table :data="permissionList" border row-key="id">
          <el-table-column label="名称">
            <template v-slot="{ row }">
              <i v-if="row.type === 1" class="el-icon-folder-opened"></i>
              {{ row.name }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="标识" prop="code"></el-table-column>
          <el-table-column align="center" label="描述" prop="description"></el-table-column>
          <el-table-column align="center" label="操作">
            <template v-slot="{ row }">
              <!-- 添加按钮 只在访问权层级显示 type=1时 操作权层级为type=2 -->
              <el-button v-if="row.type === 1" type="text" @click="addPermission(row.id, 2)">添加</el-button>
              <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
              <el-button type="text" @click="delPermission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 新增，编辑弹层 -->
    <el-dialog :title="titleText" :visible="showDialog" @close="btnCancel">
      <el-form ref="permissionForm" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width: 90%;"></el-input>
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width: 90%;"></el-input>
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" style="width: 90%;"></el-input>
        </el-form-item>
        <el-form-item label="开启">
          <el-switch v-model="formData.enVisible" active-text="1" inactive-text="0"></el-switch>
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="btnOk">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, delPermission, updatePermission, getPermissionDetail, addPermission } from '@/api/permission'
import { transListToTreeData } from '@/utils'
export default {
  data() {
    return {
      permissionList: [],
      showDialog: false,
      formData: {
        name: '',
        code: '',
        description: '',
        enVisible: '0', // 开启
        type: '',
        pid: ''
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    titleText() {
      return this.formData.id ? '编辑权限' : '新增权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const list = await getPermissionList()
      this.permissionList = transListToTreeData(list, '0')
      // console.log(this.permissionList)
    },
    async delPermission(id) {
      try {
        await this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await delPermission(id)
        this.$message.success('删除权限成功')
        this.getPermissionList()
      } catch (error) {
        console.log(error)
      }
    },
    addPermission(pid, type) {
      this.formData.pid = pid
      this.formData.type = type
      this.showDialog = true
    },
    async btnOk() {
      try {
        await this.$refs.permissionForm.validate()
        if (this.formData.id) {
          await updatePermission(this.formData)
          // 编辑
        } else {
          // 新增
          await addPermission(this.formData)
        }
        this.getPermissionList()
        this.$message.success('操作成功')
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    btnCancel() {
      this.formData = {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        type: '',
        pid: ''
      }
      this.$refs.permissionForm.resetFields()
      this.showDialog = false
    },
    async editPermission(id) {
      this.formData = await getPermissionDetail(id)
      this.showDialog = true
    }
  }
}
</script>

<style>
</style>
